package com.mdnote.structure.graph;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author Rhythm-2019
 * @version 1.0
 * @date 2020/9/6
 * @description 图
 */
public class BaseGraph {

    private List<String> vertexList;

    private int[][] edges;

    private int numOfEdge;

    public BaseGraph(int size) {
        this.vertexList = new ArrayList<>(size);
        this.edges = new int[size][size];
        this.numOfEdge = 0;
    }

    public void insertVertex(String vertex) {
        this.vertexList.add(vertex);
    }

    public void addEdge(int v1Index, int v2Index, int weight) {
        this.edges[v1Index][v2Index] = weight;
        this.edges[v2Index][v1Index] = weight;
        this.numOfEdge++;
    }

    public int vertexSize() {
        return this.vertexList.size();
    }

    public int edgeNum() {
        return this.numOfEdge;
    }

    public void showEdges() {
        for (int[] link : edges) {
            System.out.println(Arrays.toString(link));
        }
    }

    public static void main(String[] args) {
        String[] strs = new String[]{"A", "B", "C", "D", "E"};
        BaseGraph baseGraph = new BaseGraph(strs.length);
        for (String str : strs) {
            baseGraph.insertVertex(str);
        }

        baseGraph.addEdge(0, 1, 1);
        baseGraph.addEdge(0, 2, 1);
        baseGraph.addEdge(1, 2, 1);
        baseGraph.addEdge(1, 3, 1);
        baseGraph.addEdge(1, 4, 1);

        baseGraph.showEdges();
    }
}
